package content

import (
	"fmt"
	"woo-server/coms/structs"
	"woo-server/coms/utils"
	"woo-server/models/db"
)

// 用户表
type FileGroup struct {
	*structs.Common `gorm:"-" json:"-"`
	GroupId         uint64 `gorm:"primary_key;AUTO_INCREMENT;column:group_id;"` // 自增主键
	GroupType       string `gorm:"column:group_type;size:255;"`                 // 文件类型
	GroupName       string `gorm:"column:group_name;size:30"`                   // 分类名称
	Sort            int    `gorm:"column:sort;size:150;"`                       // 分类排序(数字越小越靠前)
	db.Basics
}

// 表名
func (f *FileGroup) TableName() string {
	return "woo_upload_group"
}

// 初始化
func NewFileGroup(com *structs.Common) *FileGroup {
	d := &FileGroup{}
	return d
}

func (f *FileGroup) GetConfig() structs.CFConfig {
	list := structs.CFConfig{}
	list.Column = f.Column()
	list.Forms = f.Forms()
	return list
}

func (f *FileGroup) Column() []structs.ColumnCommon {
	result := make([]structs.ColumnCommon, 0)
	result = append(result, structs.ColumnCommon{Title: "ID", DataIndex: "GroupId"})
	result = append(result, structs.ColumnCommon{Title: "分组名", DataIndex: "GroupName"})
	result = append(result, structs.ColumnCommon{Title: "分类类型", DataIndex: "GroupType"})
	result = append(result, structs.ColumnCommon{Title: "排序", DataIndex: "Sort"})
	result = append(result, structs.ColumnCommon{Title: "更新时间", DataIndex: "UpdateDate"})
	result = append(result, structs.ColumnCommon{Title: "操作", DataIndex: "Operation"})
	return result
}

func (f *FileGroup) Forms() []structs.FormsCommon {
	result := make([]structs.FormsCommon, 0)
	result = append(result, structs.FormsCommon{Key: "GroupId", Label: "主键ID", Type: "hidden", Options: []string{}})
	result = append(result, structs.FormsCommon{Key: "GroupName", Label: "分组名", Type: "input", Options: []string{}})
	result = append(result, structs.FormsCommon{Key: "GroupType", Label: "分类类型", Type: "input", Options: []string{}})
	result = append(result, structs.FormsCommon{Key: "Sort", Label: "排序", Type: "number", Options: []string{}})
	return result
}

// 创建
func (f *FileGroup) Create() error {
	return utils.GORM.Debug().Table(f.TableName()).Create(f).Error
}

// 更新
func (f *FileGroup) Save() error {
	return utils.GORM.Debug().Table(f.TableName()).Save(f).Error
}

// 批量创建
func (f *FileGroup) BatchCreate(data []*FileGroup) error {
	return utils.GORM.Debug().Table(f.TableName()).Create(data).Error
}

// 获取分页列表及总数
func (f *FileGroup) GetListAndTotal(search string, page int, pageSize int) ([]*FileGroup, int64) {
	list := make([]*FileGroup, 0)
	var total int64 = 0

	// 查询
	query := utils.GORM.Debug().Table(f.TableName())

	if len(search) > 0 {
		search = fmt.Sprintf("%%%s%%", search)
		query = query.Where("file_name like ? or group_id = ?", search, search)
	}
	query.Where("is_delete = ?", 0)
	// 总数
	query.Count(&total)

	// 列表
	offset := (page - 1) * pageSize
	query.Limit(pageSize).Offset(offset).Find(&list)

	return list, total
}

// 获取单条数据
func (f *FileGroup) GetFirst(fileId int) *FileGroup {
	//info := make([]*FileGroup, 0)

	// 查询
	query := utils.GORM.Debug().Table(f.TableName())

	query = query.Where("file_id = ?", fileId)

	// 总数
	//query.Count(&total)

	// 列表
	//offset := (page - 1) * pageSize
	query.First(&f)
	return f
}

func (f *FileGroup) Delete() error {
	return utils.GORM.Debug().Table(f.TableName()).Delete(f).Error
}
